<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\AdminUsers;
use App\Models\STeacherInfo;
use App\User;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class UserController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('学生')
			->description('列表')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('学生')
			->description('详情')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('学生')
			->description('编辑')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('学生')
			->description('新增')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid(new User);

		$grid->id('Id')->sortable();
		$grid->name('中文名');
		$grid->name_en('英文名');
		// $grid->password('Password');
		$grid->phone('电话');
		$grid->age('年龄');
		$grid->work('职业');
		// $grid->headimg('头像')->image();
		$grid->lesson_all_num('总课次');
		$grid->lesson_left_num('已上课次');
		$grid->lesson_ea_num('欧美课次');
		$grid->lesson_ph_num('菲律宾课次');
		$grid->leave_num('请假次数');
		$grid->task_num('完成任务次数');
		$grid->score_num('学习币');
		$grid->hour_num('累计说英语时间');

		$grid->adminUsers()->name('助教');
		$grid->teachers()->name('外教');

		$grid->created_at('创建时间');
		$grid->updated_at('更新时间');
		//禁用导出
		$grid->disableExport();

		//查询
		$grid->filter(function ($filter) {

			// 去掉默认的id过滤器
			// $filter->disableIdFilter();

			$filter->where(function ($query) {

				$query->whereHas('teachers', function ($query) {
					$query->where('name', 'like', "%{$this->input}%");
				});

			}, '外教');

			$filter->where(function ($query) {

				$query->whereHas('adminUsers', function ($query) {
					$query->where('name', 'like', "%{$this->input}%");
				});

			}, '助教');
		});

		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(User::findOrFail($id));

		$show->id('Id')->sortable();
		$show->name('学生姓名');
		$show->phone('电话');
		$show->headimg('头像')->image();
		$show->lesson_all_num('总课次');
		$show->lesson_left_num('已上课次');
		$show->lesson_ea_num('欧美课次');
		$show->lesson_ph_num('菲律宾课次');
		$show->leave_num('请假次数');
		$show->task_num('完成任务次数');
		$show->score_num('学习币');
		$show->hour_num('累计说英语时间');
		$show->created_at('创建时间')->sortable();
		$show->updated_at('更新时间');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new User);

		$form->text('name', '中文名');
		$form->text('name_en', '英文名');
		$form->password('password', '密码');
		$form->mobile('phone', '电话');
		$form->image('headimg', '头像');
		$form->number('age', '年龄')->min(1)->max(100);
		$form->text('work', '职业');

		// $form->number('lesson_all_num', '总课次')->min(0);
		// $form->number('lesson_left_num', '已上课次')->min(0);
		// $form->number('lesson_ea_num', '欧美课次')->min(0);
		// $form->number('lesson_ph_num', '菲律宾课次')->min(0);
		// $form->number('leave_num', '请假次数')->min(0);
		// $form->number('task_num', '完成任务次数')->min(0);
		// $form->number('score_num', '学习币')->min(0);
		// $form->number('hour_num', '累计说英语时间')->min(0);

		$admins = AdminUsers::pluck('name', 'id');
		$form->select('admin_id', '助教')->options($admins);

		$teachers = STeacherInfo::pluck('name', 'id');
		$form->select('teacher_id', '外教')->options($teachers);

		$form->saving(function (Form $form) {
			if ($form->password && $form->model()->password != $form->password) {
				$form->password = bcrypt($form->password);
			}
		});

		$form->footer(function ($footer) {

			// 去掉`查看`checkbox
			$footer->disableViewCheck();

			// 去掉`继续编辑`checkbox
			$footer->disableEditingCheck();

			// 去掉`继续创建`checkbox
			$footer->disableCreatingCheck();

		});
		return $form;
	}

}
